<?php

App::uses('AppController', 'Controller');


/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

    public $components = array('RequestHandler');
    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());

    }
    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
         $params = array(
            'download' => false,
            'name' => "teste.pdf",
            'paperOrientation' => 'landscape',
            'paperSize' => 'A4'
        );
        $this->set($params);
    }
    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            $this->request->data['User']['active'] = 1;
            //$this->request->data['User']['role'] = "admin";
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $user = $this->User->read(null, $id);
            if ($this->request->data['User']['password'] == $user['User']['password']) {
                unset($this->request->data['User']['password']); //die(var_dump($this->request->data));
                unset($this->User->data['User']['password']); //die(var_dump($this->request->data));
            }
            if(!$this->request->data['User']['role'])
                $this->request->data['User']['role'] = "teacher";
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
        }
    }

    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->User->saveField('active', 0)) {
            $this->Session->setFlash(__('User desactived'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not desactived'));
        $this->redirect(array('action' => 'index'));
    }

    public function active($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->User->saveField('active', 1)) {
            $this->Session->setFlash(__('User actived'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not actived'));
        $this->redirect(array('action' => 'index'));
    }

    public function beforeFilter() {
        parent::beforeFilter();
        //$this->Auth->allow('add'); // Letting users register themselves
        if (!($this->request->params['action'] == 'logout') && !($this->request->params['action'] == 'login') && !($this->request->params['action'] == 'changePassword'))
            $this->isAuthorized('admin');
    }

    public function login() {
        $this->layout = "login";
        if ($this->request->is('post'))
            if ($this->Auth->login()) {
                if (AuthComponent::user('active'))
                    $this->redirect($this->Auth->redirect());
                else
                    $this->Session->setFlash(__('User not active on the system'));
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
    }

    public function changePassword() {
        $user = $this->User->findById(AuthComponent::user('id'));
        if ($this->request->is('post')) {
            $this->request->data['User']['id'] = AuthComponent::user('id');
            
            if (AuthComponent::password($this->request->data['User']['passwordold']) == $user['User']['password']) {
                
                if ($this->request->data['User']['password'] == $this->request->data['User']['confirm']) {
                    if ($this->User->save($this->request->data)) {
                        $this->Session->setFlash(__('The user password has been saved'));
                        $this->redirect('/');
                    } else {
                        $this->Session->setFlash(__('The user password could not be saved. Please, try again.'));
                        $this->redirect(array('action' => 'changePassword'));
                    }
                }else
                    $this->Session->setFlash(__('The new password dont match with your confirm. Please, try again.'));
                    $this->redirect(array('action' => 'changePassword'));
            } else {
                 $this->Session->setFlash(__('The old password dont match. Please, try again.'));
                 $this->redirect(array('action' => 'changePassword'));
            }
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }

}
